package com.example.winner.handlerthread;

import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.Button;
import android.view.View.OnClickListener;
import android.view.View;


public class MainActivity extends AppCompatActivity {
    private Button startbt=null;
    private Button endbt=null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        startbt=(Button)findViewById(R.id.start);
        endbt=(Button)findViewById(R.id.end);
        startbt.setOnClickListener(new StartbtButtonListener());
        endbt.setOnClickListener(new EndbtButtonListener());
    }
    Handler handler=new Handler();
    class StartbtButtonListener implements OnClickListener
    {
        @Override
        public void onClick(View view)
        {
            handler.post(updateThread);
        }
    }
    class EndbtButtonListener implements OnClickListener
    {
        @Override
        public void onClick(View view)
        {
            handler.removeCallbacks(updateThread);
        }
    }
    Runnable updateThread=new Runnable() {
        @Override
        public void run() {
            System.out.println("UpdateThread");
            Log.v("tag","UpdateThread");
            handler.postDelayed(updateThread,2000);
        }
    };
}
